package Controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class ConnectToServerListener implements ActionListener {
	private Controler controler;
	
	public ConnectToServerListener(Controler controler){
		this.controler = controler;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String line;
		while(true)
		{
			line = (String)JOptionPane.showInputDialog(null,
                    "Entrer l'IP et le port :\n",
                    "Lancer le serveur",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    "127.0.0.1:2222");
			if(line == null)
			{
				break;
			}
			if(line.isEmpty()) line = "127.0.0.1:2222";
			
			if(line.split(":").length == 2)
			{
				break;
			}
		}
		
		if(line != null)
		{
			String ip = line.split(":")[0];
			String port = line.split(":")[1];
			int intPort;
			try{
				intPort = Integer.parseInt(port);
				controler.connectToServer(ip, intPort);
			}catch(Exception ex){
				System.err.println("Problème parsage port");
			}
		}
	}

}
